﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.ObjectMapping;
using WMS.Common;
using WMS.RepositoryDefinitions;
using WMS.RepositoryDefinitions.Dto;

namespace WMS.RepositoryDefinitionServices
{
    public class RepositoryDefinitionService : ApplicationService, IRepositoryDefinitionService
    {
        private readonly IRepository<RepositoryDefinition, int> _repository;

        public RepositoryDefinitionService(IRepository<RepositoryDefinition, int> repository)
        {
            _repository = repository;
        }

        public async Task<int> AddRepository(RepositoryDefinitionDto repositoryDefinitionDto)
        {
            var repositoryCode = _repository.AnyAsync(m => m.RepositoryCode == repositoryDefinitionDto.RepositoryCode).Result;
            if (repositoryCode)
            {
                return -1;
            }

            var repositoryName = _repository.AnyAsync(m => m.RepositoryName == repositoryDefinitionDto.RepositoryName).Result;
            if (repositoryName)
            {
                return -1;
            }

            var repositoryModel = repositoryDefinitionDto.MapTo<RepositoryDefinition>();
            await _repository.InsertAsync(repositoryModel);
            return repositoryDefinitionDto.Id;
        }

        public async Task<PagedResult> GetRepositoryList(string? repositoryCode, int page = 1, int limit = 10)
        {
            var predicate = PredicateBuilder.New<RepositoryDefinition>(true);
            if (!string.IsNullOrEmpty(repositoryCode))
            {
                predicate = predicate.And(m => m.RepositoryCode.Contains(repositoryCode));
            }
            var dto = _repository.GetListAsync(predicate).Result;
            PagedResult result = new PagedResult();
            result.PageCount = (int)Math.Ceiling((decimal)(dto.Count == 0 ? 1 : dto.Count) / limit);
            result.PageSize = limit;
            result.CurrentPage = page;
            result.RowCount = dto.Count;
            result.Queryable = dto.Skip((page - 1) * limit).Take(limit).AsQueryable();
            return result;
        }

        public async Task<int> DeleteRepository(int id)
        {
            await _repository.DeleteAsync(id);
            return 1;
        }

        public async Task<RepositoryDefinitionDto> GetRepositoryInfo(int id)
        {
            var entity = _repository.GetAsync(id).Result;
            var repositoryDefinitionDto = ObjectMapper.Map<RepositoryDefinition, RepositoryDefinitionDto>(entity);
            return repositoryDefinitionDto;
        }

        public async Task<int> UpdateRepository(RepositoryDefinitionDto repositoryDefinitionDto)
        {
            var repositoryName = await _repository.AnyAsync(m => m.RepositoryName == repositoryDefinitionDto.RepositoryName && m.Id != repositoryDefinitionDto.Id);

            RepositoryDefinition repositoryDefinition = repositoryDefinitionDto.MapTo<RepositoryDefinition>();
            await _repository.UpdateAsync(repositoryDefinition);
            return repositoryDefinition.Id;
        }
    }
}
